package com.foreveross.project.projectonline.web.controller;

import java.util.Enumeration;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.foreveross.project.projectonline.config.NeedLogin;
import com.foreveross.project.projectonline.model.Project;
import com.foreveross.project.projectonline.service.MgtUserService;
import com.foreveross.project.projectonline.service.ProjectService;
import com.jfinal.aop.Before;
import com.jfinal.plugin.spring.Inject;
import com.jfinal.plugin.spring.IocInterceptor;

/**
 * 
 * @author Administrator
 *
 */
@Before(IocInterceptor.class)
@NeedLogin
public class ProjectController extends BaseController {

	@Inject.BY_NAME
	ProjectService projectService;

	public void index() {
		List<Project> list = projectService.selectProjectByContition();
		setAttr("list", list);

		/*
		 * for (int i = 0; i < list.size(); i++) {
		 * 
		 * }
		 */

		render("/pages/project.html");
	}

	public void project() {
		List<Project> list = projectService.selectProjectByContition();
		setAttr("list", list);
		index();
	}

	public void edit() {

		String id = getPara(0);

		System.out.println(id);
		Project project = projectService.getProjectById(id);
		setAttr("project",project);
		index();
	}

	public void delete() {

		String id = getPara(0);

		System.out.println(id);
		
		projectService.deleteProject(id);
		
		index();
	}
	
	public void create(){
		
		/*String name = getPara("name");
		String showName = getPara("showName");
		String groupId = getPara("groupId");
		String artifactId = getPara("artifactId");
		String version = getPara("version");
		String basePackage = getPara("package");
		String type = getPara("type");
		String description = getPara("description");
		String gitUrl = getPara("gitUrl");
		String gitUsername = getPara("gitUsername");
		String gitPassword = getPara("gitPassword");
		String gitPort = getPara("gitPort");
		String gitConnType = getPara("gitConnType");
		String gitKey = getPara("gitKey");
		String dbHost = getPara("dbHost");
		String dbUsername = getPara("dbUsername");
		String dbPassword = getPara("dbPassword");
		String dbPort = getPara("dbPort");
		String dbType = getPara("dbType");
		String dbName = getPara("dbName");
		*/
		
		Project project = new Project();
		
		Enumeration<String> paraNames = getParaNames();
		while(paraNames.hasMoreElements()){
			
		       String paraName = paraNames.nextElement().toString();
		       String paraValue = getPara(paraName);
		     
		       if("USER_ID".equals(paraName)&&StringUtils.isEmpty(paraValue)){
		    	   paraValue = null;
		       }
		       if("GIT_PORT".equals(paraName)&&StringUtils.isEmpty(paraValue)){
		    	   paraValue = null;
		       }
		       if("DB_PORT".equals(paraName)&&StringUtils.isEmpty(paraValue)){
		    	   paraValue = null;
		       }
		       project.set(paraName, paraValue);
		}
		//String message = "SUCCESS";
		String message = projectService.createProject(project);
		
		setAttr("message", message);
		render("/pages/createProject.html");
	}
	
	public void createIndex(){
		render("/pages/createProject.html");
	}
	
}
